

package com.gamutsoft.amb.common.util;

import javax.servlet.http.HttpServletRequest;

import org.extremecomponents.table.context.Context;
import org.extremecomponents.table.context.HttpServletRequestContext;
import org.extremecomponents.table.core.TableConstants;
import org.extremecomponents.table.limit.Limit;
import org.extremecomponents.table.limit.LimitFactory;
import org.extremecomponents.table.limit.TableLimit;
import org.extremecomponents.table.limit.TableLimitFactory;


/**
 * <h3>Class name</h3>
 * ec组件的工具类
 * <h4>Description</h4>
 * <h4>Special Notes</h4>
 * 
 * @ver 0.1
 * @author Terry.wei
 */
public class ExtremeTablePage
{

	public static Limit getLimit(HttpServletRequest request)
	{
		Context context =new HttpServletRequestContext( request );
		LimitFactory limitFactory = new TableLimitFactory(context);
		TableLimit limit = new TableLimit( limitFactory );
		limit.setRowAttributes( Integer.MAX_VALUE, 20 );
		return limit;
	}

	public static String getTableId(HttpServletRequest request)
	{
		return getTableId( request, null );
	}

	public static int getTotalRowsFromRequest(HttpServletRequest request)
	{
		return getTotalRowsFromRequest( request, null );
	}

	public static int getTotalRowsFromRequest(HttpServletRequest request, String cTableId)
	{
		int totalRows = -1;
		String tableId = getTableId( request, cTableId );
		tableId = tableId == null ? "" : tableId + "_";
		Integer totalRowsI = (Integer) request.getAttribute( tableId + TableConstants.TOTAL_ROWS );
		try
		{
			if ( totalRowsI != null )
			{
				totalRows = totalRowsI.intValue();
			} else
			{
				String totalRowsS = request.getParameter( tableId + "totalrows" );
				totalRows = Integer.parseInt( totalRowsS );
			}
		} catch ( Exception e )
		{
			totalRows = -1;
		}
		totalRows = totalRows < 0 ? -1 : totalRows;
		return totalRows;
	}

	public static String getTableId(HttpServletRequest request, String cTableId)
	{
		if ( cTableId != null )
		{
			return cTableId;
		}
		String tableId = request.getParameter( TableConstants.EXTREME_COMPONENTS_INSTANCE );
		if ( tableId == null )
		{
			tableId = TableConstants.EXTREME_COMPONENTS;
		}
		return tableId;
	}
}
